package UserInterface;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import HostComm.*;
import java.awt.GridLayout;
import java.util.Iterator;
import javax.comm.CommPortIdentifier;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

/**
 *
 * @author Dakota Brown, Travis Shuff
 * @since 10/11/11
 * 
 */
public final class Host_View extends JFrame {

    private static Host_View view;
    
    //Host and Queue
    private Host host = new Host(this);
    
    //JFrame Components:
    //1.) Menu
    private JMenuBar menuBar = new JMenuBar();
    private JMenu fileMenu = new JMenu("File");
    private JMenuItem connectionSettingItem = new JMenuItem("Connection Settings");
    private JMenuItem connect = new JMenuItem("Connect");
    //setting dialogs
    private ConnectionSettingsDialog connSett = new ConnectionSettingsDialog(host.connObj);
    //2.) Body
    private JButton jbtnClose = new JButton("Close");
    private static JTextField jfieldRobots = new JTextField();
    private static JTextArea status = new JTextArea();

    /**
     * Host view construct
     * Creates window with status area and close button
     * along with an instance of Host and QueueHost
     * 
     * @since 10/18/11 
     * 
     */
    public Host_View() {
        this.setSize(400, 400);
        this.setLayout(new BorderLayout());

        fileMenu.add(connectionSettingItem);
        fileMenu.add(connect);
        menuBar.add(fileMenu);
        jfieldRobots.setEditable(false);
        status.setEditable(false);

        JPanel panel = new JPanel();

        panel.add(jfieldRobots);
        panel.add(status);

        setRobotCount(0);

        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.add(menuBar, BorderLayout.NORTH);
        this.add(panel, BorderLayout.CENTER);
        this.add(jbtnClose, BorderLayout.SOUTH);

        this.setVisible(true);

        //"Connection Settings": Opens JFrame
        connectionSettingItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                connSett.setVisible(true);
            }
        });

        //"Connect": Attempts connection to database if Connection Object is initialized
        connect.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (host.connObj != null) {
                    displayStatus(host.connect(host.connObj));
                    if (host.isConnected()) {
                        host.startUp();
                        validate();
                    }
                } else {
                    displayStatus("Check Connection Settings");
                }
            }
        });

        jbtnClose.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                host.closeHost();
                host.queueHost.stop();
            }
        });
    }

    /**
     * Creates a new  instance of Host_View
     * 
     * @param args 
     * 
     * @since 10/18/11
     */
    public static void main(String[] args) {
        view = new Host_View();
    } 

    /**
     * Sets the number of active Robots
     * 
     * @param count the number of Robots
     * 
     * @since 10/11/11
     */
    public  void setRobotCount(int count) {
        jfieldRobots.setText("Active Robots " + count);

    }

    /**
     * Appends a String to the TextArea
     * 
     * @param status message to be appended to TextArea
     * 
     * @since 10/11/11
     */
    public void displayStatus(String status) {
        Host_View.status.append(status + "\n");

    }
    //end host_views's methods   

    /**
     * Connection Settings Window
     * 
     * @since 10/18/11
     */
    private static class ConnectionSettingsDialog extends JFrame {

        //Connection Object
        HostConnectionObject conn;
        //UI Variables
        private JPanel layoutPanel = new JPanel();
        private JLabel connLabel = new JLabel("Host Address: ");
        private JTextField hostAdd = new JTextField(15);
        private JLabel nameLabel = new JLabel("User Name: ");
        private JTextField userName = new JTextField(15);
        private JLabel passLabel = new JLabel("Password: ");
        private JPasswordField password = new JPasswordField(15);
        private JLabel portLabel = new JLabel("Port: ");
        private JTextField portNum = new JTextField(4);
        private JButton submit = new JButton("Submit");

        public ConnectionSettingsDialog(HostConnectionObject conObj) {
            conn = conObj;

            //loads prior connect settings if object is not null pointer
            if (conn != null) {
                hostAdd.setText(conn.getHost());
                userName.setText(conn.getUser());
                password.setText(conn.getPass());
                portNum.setText(conn.getPort());
            }

            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            layoutPanel.setLayout(new GridLayout(0, 2));
            layoutPanel.add(connLabel);
            layoutPanel.add(hostAdd);
            layoutPanel.add(nameLabel);
            layoutPanel.add(userName);
            layoutPanel.add(passLabel);
            layoutPanel.add(password);
            layoutPanel.add(portLabel);
            layoutPanel.add(portNum);

            //adds ActionListener to 
            submit.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    conn = getConnectSettings();
                    setVisible(false);
                }
            });
            layoutPanel.add(submit);

            setContentPane(layoutPanel);
            setLocationRelativeTo(null);
            pack();
            setVisible(false);
        }

        /**
         * Gets new HostConnectionObject
         * 
         * @return new HostConnectionObject from current settings
         * @since 10/6/11
         */
        public HostConnectionObject getConnectSettings() {
            return new HostConnectionObject(hostAdd.getText(),
                    userName.getText(),
                    new String(password.getPassword()),
                    portNum.getText());
        }
    } // end ConnectionSettingsDialog
}
